package com.aidex.biz.controller;

import com.aidex.biz.domain.*;
import com.aidex.biz.domain.enums.CustomerEnum;
import com.aidex.biz.service.*;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.StringUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.aidex.common.utils.sms.ChuangLanSmsClient;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 客户跟踪Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2022-08-06
 */
@RestController
@RequestMapping("/biz/bizCustomerShadowInfo")
public class CustomerShadowInfoController extends BaseController {
    @Autowired
    private CustomerShadowInfoService customerShadowInfoService;
    @Autowired
    private CustomerService customerInfoService;
    @Autowired
    private ToHomeService toHomeService;

    @Autowired
    private CustomerXiezuService customerXiezuService;
    @Autowired
    private CustomerDongtaiService customerDongtaiService;

    /**
     * 查询客户跟踪列表
     */
    @GetMapping("/list")
    public R<PageInfo> list(CustomerShadowInfo customerShadowInfo, HttpServletRequest request,
                            HttpServletResponse response) {
//        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
//            customerShadowInfo.setCompanyId(null);
//        } else {
//            customerShadowInfo.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
//        }
        customerShadowInfo.setPage(new PageDomain(request, response));
        return R.data(customerShadowInfoService.findMylist(customerShadowInfo));
    }

    /**
     * 查询客户跟踪列表
     */
    @GetMapping("/mylist")
    public R<PageInfo> mylist(CustomerShadowInfo customerShadowInfo, HttpServletRequest request,
                              HttpServletResponse response) {
        customerShadowInfo.setPage(new PageDomain(request, response));
        return R.data(customerShadowInfoService.findPage(customerShadowInfo));
    }

    /**
     * 获取客户跟踪详细信息
     */
    @GetMapping(value = "/{id}")
    public R<CustomerShadowInfo> detail(@PathVariable("id") String id) {
        return R.data(customerShadowInfoService.get(id));
    }

    /**
     * 新增客户跟踪
     */
    @Log(title = "新增客户跟踪", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated CustomerShadowInfo customerShadowInfo) throws ParseException {
        return R.status(customerShadowInfoService.addCustomerShadowInfo(customerShadowInfo));
    }

    /**
     * 修改客户跟踪
     */
    @Log(title = "客户跟踪", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated CustomerShadowInfo customerShadowInfo) {
        return R.status(customerShadowInfoService.save(customerShadowInfo));
    }


    /**
     * 客户跟踪
     */
    @Log(title = "客户跟踪", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(customerShadowInfoService.deleteCustomerShadowInfoByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(customerShadowInfoService.findMaxSort(new CustomerShadowInfo()));
    }

    /**
     * 导出客户跟踪列表
     */
    @Log(title = "客户跟踪", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(CustomerShadowInfo customerShadowInfo) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            customerShadowInfo.setCompanyId(null);
        } else {
            customerShadowInfo.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<CustomerShadowInfo> list = customerShadowInfoService.findList(customerShadowInfo);
        ExcelUtil<CustomerShadowInfo> util = new ExcelUtil<CustomerShadowInfo>(CustomerShadowInfo.class);
        return util.exportExcel(list, "客户跟踪数据");
    }

}
